package hot100;

import java.util.HashMap;
import java.util.Map;

public class copyRandomList2 {
    public Node copyRandomList(Node head) {
        if(head == null) {
            return null;
        }
        Node cur = head;
        Map<Node,Node> map = new HashMap<>();
        //复制节点，创建 原节点->新节点 的映射
        while(cur != null) {
            map.put(cur,new Node(cur.val));
            cur = cur.next;
        }
        cur = head;
        while(cur != null) {
            map.get(cur).next = map.get(cur.next);
            map.get(cur).random = map.get(cur.random);
            cur = cur.next;
        }
        return map.get(head);
    }
}
